package org.jeecg.glr.api.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.glr.api.constant.ApiConstant;
import org.jeecg.glr.service.IGlrVideoBdService;
import org.jeecg.glr.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 外部调用
 * 海康盒子调用传送抓拍的流量信息
 */
@Slf4j
@Controller
@RequestMapping(value = "/apiGlr/HkBox")
public class ApiHkBoxController {
    @Value(value = "${jeecg.path.facehomedirectory}")
    private String uploadpath;

    @Autowired
    private IGlrVideoBdService glrVideoBdService;

    @RequestMapping("/videoData")
    @ResponseBody
    public Map<String, Object> fileface(HttpServletRequest request, HttpServletResponse response, String code) throws  Exception{
        Map<String, Object> resMap = new HashMap<String, Object>();
        String MessageID="";
        String SerialNumber="";
        try{
            // 参数判断
            String data = request.getParameter("data");
            if(StringUtil.isEmpty(data))
            {
                resMap.put("resCode", ApiConstant.FAIL);
                resMap.put("resMsg", ApiConstant.PARAM_NOTNULL_MSG);
                return resMap;
            }

            /*
            data="{\n" +
                    "\"SerialNumber\":\"DS-2CD7187FWD-IZ20190603AACHD26206859\",\n" +
                    "\"IpAddress\":\"192.168.5.244\",\n" +
                    "\"SNapTime\":\"2019-08-28 09:03:05\",\n" +
                    "\"ChannelID\":\"1\",\n" +
                    "\"PortNo\":\"8000\",\n" +
                    "\"MessageID\":\"20190828010828254\",\n" +
                    "\"CaptureResult\":\t\n" +
                    "[{\n" +
                    "\"human_data\":\t\n" +
                    "[{\n" +
                    "\"CoatColor\":\"gray\",\n" +
                    "\"ByGlasses\":\"yes\",\n" +
                    "\"Knapsack\":\"no\",\n" +
                    "\"LiftAnything\":\"unknown\",\n" +
                    "\"ByHat\":\"no\",\n" +
                    "\"ByMask\":\"no\"\n" +
                    "}]\n" +
                    "}]\n" +
                    "}"  ;
                    */
            Map<String, Object> stringObjectMap = JSON.parseObject(data, Map.class);
            MessageID=stringObjectMap.get("MessageID").toString();
            SerialNumber=stringObjectMap.get("SerialNumber").toString();
            //DS-2CD7A47HWD-XZ20190905AACHD59087135
            //DS-2CD7187FWD-IZ20190603AACHD26206859
            //D69211791
            //只取最后9位
            int n=9;
            if (SerialNumber.length()>=n){
                SerialNumber=SerialNumber.substring(SerialNumber.length()-n,SerialNumber.length());
            }
            String  stSNapTime=stringObjectMap.get("SNapTime").toString();
            JSONArray CaptureResult=(JSONArray)stringObjectMap.get("CaptureResult");
            if (CaptureResult.size()>0){
                for(int i=0;i<CaptureResult.size();i++){
                    JSONObject jsonObjectReturn=new JSONObject();

                    JSONObject jsonObject=(JSONObject)CaptureResult.get(i);

                    JSONArray human_data=(JSONArray)jsonObject.get("human_data");

                    if (human_data!=null&&human_data.size()>0){
                        for(int j=0;j<human_data.size();j++){
                            JSONObject jsonObjectj11=(JSONObject)human_data.get(j);

                            //0-未知  1-男 2-女
                            Object oBySex=jsonObjectj11.get("BySex");
                            if (oBySex==null) oBySex="0";
                            if ("unknown".equals(oBySex)) oBySex="0";
                            if ("male".equals(oBySex)) oBySex="1";
                            if ("female".equals(oBySex)) oBySex="2";
                            jsonObjectReturn.put("BySex",oBySex.toString());

                            jsonObjectReturn.put("type","human");

                            doSave(SerialNumber,MessageID, stSNapTime,jsonObjectReturn,i);
                        }
                    }

                    JSONArray nonmotor_data=(JSONArray)jsonObject.get("nonmotor_data");
                    if (nonmotor_data!=null&&nonmotor_data.size()>0){
                        for(int j=0;j<nonmotor_data.size();j++){
                            JSONObject jsonObjectj11=(JSONObject)nonmotor_data.get(j);

                            //0-未知  1-男 2-女
                            Object oBySex=jsonObjectj11.get("BySex");
                            if (oBySex==null) oBySex="0";
                            if ("unknown".equals(oBySex)) oBySex="0";
                            if ("male".equals(oBySex)) oBySex="1";
                            if ("female".equals(oBySex)) oBySex="2";
                            jsonObjectReturn.put("BySex",oBySex.toString());

                            jsonObjectReturn.put("type","nonmotor");

                            doSave(SerialNumber,MessageID, stSNapTime,jsonObjectReturn,i);
                        }
                    }

                    JSONArray motor_data=(JSONArray)jsonObject.get("motor_data");
                    if (motor_data!=null&&motor_data.size()>0){
                        for(int j=0;j<motor_data.size();j++){
                            JSONObject jsonObjectj11=(JSONObject)motor_data.get(j);

                            jsonObjectReturn.put("type","motor");

                            doSave(SerialNumber,MessageID, stSNapTime,jsonObjectReturn,i);
                        }
                    }



                }
            }

            //stringObjectMap.get("CaptureResult");
            //Map<String, Object> CaptureResult = JSON.parseObject(stringObjectMap.get("CaptureResult").toString(), Map.class);

            /*
            JSONObject jsonObject = JSONObject.fromObject(data);
            if (jsonObject==null) throw new Exception("不能转成json");
            //MessageID=object.getString("MessageID");

            Map<String, Class> classMap = new HashMap<String, Class>();
            classMap.put("captureResult", CaptureResult.class);
            classMap.put("human_data", Human.class);
            classMap.put("nonmotor_data", Nonmotor.class);
            classMap.put("motor_data", Motor.class);

            HkBoxData stud = (HkBoxData) JSONObject.toBean(jsonObject,HkBoxData.class,classMap);
             */

            resMap.put("MessageID", MessageID);
            resMap.put("resCode", ApiConstant.SUCCESS);
            resMap.put("resMsg", ApiConstant.SUCCESS_MSG);


        }catch (Exception e){
            resMap.put("MessageID", MessageID);
            resMap.put("resCode", ApiConstant.FAIL);
            resMap.put("resMsg", e.getMessage());
        }
        return resMap;
    }

    public void doSave(String SerialNumber,String MessageID,String  stSNapTime,JSONObject jsonObjectReturn,int i){
        jsonObjectReturn.put("SerialNumber", SerialNumber);

        jsonObjectReturn.put("MessageID", MessageID);
        try {
            SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date date=sDateFormat.parse(stSNapTime);
            jsonObjectReturn.put("SNapTime",date);
        } catch(ParseException px) {
            px.printStackTrace();
        }
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String stSNapTime1=SerialNumber+"--"+stSNapTime+"--"+new DecimalFormat("0000").format(i)+"--"+simpleDateFormat.format(new Date());
        synchronized (ApiConstant.HK_BOX_DATA) {
            ApiConstant.HK_BOX_DATA.put(stSNapTime1,jsonObjectReturn);
        }


    }
}
